<?php

namespace RTSCraft\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use RTSCraft\MainBundle\Entity\Article;
use RTSCraft\MainBundle\Entity\Version;

use RTSCraft\MainBundle\Controller\Util\AuthUtil;

class EditionController extends Controller
{
    /**
     * @Route("/", name="edition")
     * @Template()
     */
    public function indexAction()
    {
      // index page
      $auth = AuthUtil::is_auth($this);
      if ($auth > 0)
	{
	  $error = 'Not connected.';
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => $auth));
	}
      return $this->render('RTSCraftMainBundle:Admin:edition.html.twig');
    }

    public function postAction()
    {
      $request = $this->get('request');
      $text = '';
      $title = '';

      if ($request->getMethod() == 'POST')
	{
	  $title = $request->get('title');
	  $text = $request->get('text');
	}

      if (($title == '') || ($text == ''))
	{
	  $error = "No data submitted";
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => '107'));
	}

      /* ----------------------------------- **
      ** check if the article already exists **
      ** ----------------------------------- */

      $rep = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Article');
      $articles = $rep->findBy(array('title' => $title));

      if (!empty($articles))
	{
	  //yes ...
	  $error = "Article already exists... choose a different title";
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => '108'));
	}
      $text = $this->parse_msg($text);

      $em = $this->getDoctrine()->getEntityManager();

      //insert into articles
      $article = new Article();

      /* FIXME : element ? */

      $article->setTitle($title);
      $em->persist($article);
      $em->flush();

      //first version
      $version = new Version();
      $version->setNumVersion('1.0');
      $version->setAuthor('admin'); /* ? */
      $version->setIp($_SERVER['REMOTE_ADDR']);
      $version->setDate(new \DateTime('now'));
      $version->setText($text);
      $version->setArticleId($article);

      $em->persist($version);
      $em->flush();

      return new Response($title . ' : ' . $text);
    }

    private function parse_msg($msg)
    {
      /* FIXME : parse message if needed */
      return $msg;
    }

    public function editAction($name)
    {
      $auth = AuthUtil::is_auth($this);
      if ($auth > 0)
	{
	  $error = 'Not connected.';
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => $auth));
	}

      $em = $this->getDoctrine()->getEntityManager();
      $rep = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Article');
      $article = $rep->getLastVersion($name);

      /* FIXME : check for NULL */
      $text = $article->getText();

      return $this->render('RTSCraftMainBundle:Admin:edition.html.twig', array('title' => $name, 'text' => $text));
    }

    public function editpostAction()
    {
      $auth = AuthUtil::is_auth($this);
      if ($auth > 0)
	{
	  $error = 'Not connected.';
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => $auth));
	}

      $title = '';
      $text = '';

      $request = $this->get('request');

      if ($request->getMethod() == 'POST')
	{
	  $title = $request->get('title');
	  $text = $request->get('text');
	}
      else
	{
	  $error = 'No data submitted';
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => '106'));
	}


      /* I suppose that the user is not *that* stupid */
      $em = $this->getDoctrine()->getEntityManager();
      $rep = $this->getDoctrine()->getRepository('RTSCraftMainBundle:Article');
      $last_version = $rep->getLastVersion($title);

      if ($last_version == NULL)
	{
	  $error = 'Article not found';
	  return $this->render('RTSCraftMainBundle:Common:error.html.twig', array('error' => $error, 'code' => 112));
	}

      $new_version = new Version();
      $last_version_number = floatval($last_version->getNumVersion());
      $this_version = strval($last_version_number + 0.1);


      $new_version->setArticleId($last_version->getArticleId());
      $new_version->setNumVersion($this_version);
      $new_version->setAuthor('admin'); /* fixme if needed */
      $new_version->setIp($_SERVER['REMOTE_ADDR']);
      $new_version->setDate(new \DateTime('now'));
      $new_version->setText($text);

      $em->persist($new_version);
      $em->flush();

      //echo '<script type="text/javascript">submitForm("article/' . $title . '")</script>';
      return new Response('ok');
    }
}